import org.junit.Test;

public class StringTest {
    @Test
    public void show1() {
        //字符串常量的内容都是相同的地址，直接引用，其他的直接创建新的
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1 == s2);

        s2 = "hi";
        System.out.println(s1 == s2);

        s2 = "hello";
        System.out.println(s1 == s2);

        //新建的都是全新的对象
        String s3 = new String("hello");
        System.out.println(s1 == s3);
    }

    @Test
    public void show2() {
        //如果变量加一个final，就会变成常量，下面的s1参与的计算就等于常量计算了
        String s1 = "hello";
        String s2 = "world";
        String s3 = "helloworld";
        String s4 = "hello" + "world";
        String s5 = s1 + "world";
        String s6 = "hello" + s2;
        String s7 = s1 + s2;

        System.out.println(s3 == s4);  //true，常量的合并还是等于常量，有变量参与都会新new一个对象
        System.out.println(s3 == s5);  //false
        System.out.println(s3 == s6);  //false
        System.out.println(s3 == s7);  //false
        System.out.println(s5 == s6);  //false
        System.out.println(s5 == s7);  //false
        System.out.println("--------------------------");

        //返回字符串常量中的字面量地址
        String s8 = s5.intern();
        System.out.println(s3 == s8);

        //组合字符串，都是生成新的对象
        String s9 = "hello".concat(s2);
        System.out.println(s9 == s3);
    }
}
